﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.APIs.ZKT;
using Waker.Common;

namespace Waker.Cards
{
    public partial class FormWagesAdd : Base.FormBasePop
    {
        //ZKTHelper zktHelp;
        int _FingerId;
        DateTime _FingerTime;
        string _EmpID;
        public FormWagesAdd(string depName, string empId, int fingerId, string name, string no, string idCard)
        {
            if (fingerId < 1 || string.IsNullOrEmpty(empId))
                this.DialogResult = DialogResult.Cancel;
            InitializeComponent();
            txtDept_Name.Text = depName;
            txtEmp_Name.Text = name;
            txtEmp_No.Text = no;
            txtEmp_IDCard.Text = idCard;
            _FingerId = fingerId;
            _EmpID = empId;
            lblFingerID.Text = fingerId.ToString();
        }
        private void FormWagesAdd_Load(object sender, EventArgs e)
        {
            try
            {
                StartWaiting();
                for (int i = DateTime.Now.Year - 10; i < 10; i++)
                {
                    cbeWagesYear.Properties.Items.Add(i);
                }
                cbeWagesYear.EditValue = DateTime.Now.Year;
                for (int i = 1; i <= 12; i++)
                {
                    cbeWagesMonth.Properties.Items.Add(i);
                }
                cbeWagesMonth.EditValue = DateTime.Now.Month;
                //zktHelp = new ZKTHelper();
                if (!Session.Finger_IsConneted)
                {
                    var res = ZKTHelper.ZKTConnect();
                    Session.Finger_IsConneted = res.IsSuccess;
                    if (!res.IsSuccess)
                    {
                        lblFingerStatus.Text = res.Message;
                        return;
                    }
                }

                lblFingerStatus.Text = "已经连接";

                ZKTHelper.RegEvntOnAttTransaction(
                    (int EnrollNumber, int IsInValid, int AttState, int VerifyMethod, int Year, int Month, int Day, int Hour, int Minute, int Second) =>
                    {
                        // EnrollNumber为该用户的UserID IsInValid 为该记录是否为有效记录，1为无效记录，0为有效记录。 AttState为考勤状态 默认0—Check-In 4—OT-In 1—Check-Out2—Break-Out3—Break-In 5—OT-Out VerifyMethod为验证方式 一般情况下：0为密码验证，1为指纹验证，2为卡验证
                        // Year/Month/Day/Hour/Minute/Second为验证通过时的时间
                        if (IsInValid != 0)
                        {
                            lblValidStatus.Text = "无效记录,确实失败,请重试";
                            lblValidStatus.Tag = false;
                            return;
                        }
                        if (EnrollNumber != _FingerId)
                        {
                            lblValidStatus.Text = "员工不匹配,确实失败,请重试";
                            lblValidStatus.Tag = false;
                            return;
                        }
                        //lblValidStatus.Text = string.Format("确认成功,时间:{0}:{1}:{2}", Hour, Minute, Second);
                        _FingerTime = new DateTime(Year, Month, Day, Hour, Minute, Second);
                        lblValidStatus.Text = string.Format("确认成功,时间:{0}", _FingerTime.ToLongTimeString());
                        lblValidStatus.Tag = true;
                    });
                //ZKTHelper.RegEvntOnAttTransactionEx(
                //    (string EnrollNumber, int IsInValid, int AttState, int VerifyMethod, int Year, int Month, int Day, int Hour, int Minute, int Second, int WorkCode) =>
                //    {
                //        UI.ShowInfo("b");
                //    });
            }
            catch (Exception ex)
            {
                UI.ShowError(ex.Message);
            }
            finally
            {
                EndWaiting();
            }
        }

        //protected override void FormClose()
        //{
        //    zktHelp = null;
        //    base.FormClose();
        //}
        protected override void FormSave()
        {
            base.errorPop.ClearErrors();
            if (cbeWagesYear.EditValue == null || cbeWagesYear.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(cbeWagesYear, "请选择年份");
                return;
            }
            if (cbeWagesMonth.EditValue == null || cbeWagesMonth.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(cbeWagesMonth, "请选择年份");
                return;
            }
            if (!Convert.ToBoolean(lblValidStatus.Tag) || _FingerTime == null)
            {
                UI.ShowError("请在指纹机上确认指纹后重试");
                return;
            }
            if (!UI.ShowOKCancelReturnBool(string.Format("工资确认:{0}年{1}月员工[{2}]的工资,确定之后不能修改,确定继续?", cbeWagesYear.EditValue, cbeWagesMonth.EditValue, txtEmp_Name.Text)))
                return;
            var res = new BAL.BAL_Cards().SaveWages(_EmpID, Convert.ToInt32(cbeWagesYear.EditValue), Convert.ToInt32(cbeWagesMonth.EditValue), _FingerTime);
            if (res.IsSuccess)
            {
                UI.ShowInfo("保存成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                UI.ShowError(res.Message);
            }
        }
    }
}
